<?
class Tbl_foto extends CI_Model {
    var $tabel;

    function __construct(){
         // Call the Model constructor
         parent::__construct();
		 $this->table ="tbl_foto";
     }

	function load_foto($id) {
	$posisi = $this->uri->segment(5);
    if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT * FROM ".$this->table." WHERE album_id=$id ORDER BY id ASC LIMIT $posisi,15");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT * FROM ".$this->table." WHERE album_id=$id ORDER BY id ASC LIMIT $posisi,15");
		return $query->result();	
	}
	
	
	function loadFotoAll($id) {
	  $query=$this->db->query("SELECT * FROM ".$this->table." WHERE album_id=$id ORDER BY id ASC");
      return $query->result();
    }
	 
	function hitung_foto($id){
		$data = $this->db->query("SELECT * FROM ".$this->table." WHERE album_id=$id");
		$banyak= $this->db->affected_rows($data);
		return $banyak;
	}
	
	function add_foto($nama, $deskripsi, $url, $org_name, $size_name, $album_id, $slideshow){

		$array = array('nama_gambar'    => $nama,
					   'deskripsi' 		=> $deskripsi,
					   'org_name' 		=> $org_name,
					   'size_gambar'	=> $size_name,
					   'url_gambar' 	=> $url,
					   'album_id'  		=> $album_id,
					   'slideshow'  	=> $slideshow
					   );
		$this->db->insert($this->table, $array);
	 }
	
	function delete_foto($id){
		$this->db->query("DELETE FROM ".$this->table." WHERE id=$id");

	}
	
	function org_name_foto($id){
		$query= $this->db->query("SELECT org_name FROM ".$this->table." WHERE id=$id");
		$field = $query->row(); 
				$org_name = $field->org_name;
		return $org_name;
	}

	function update_foto($id, $nama, $deskripsi,$slideshow){
		$this->db->query("UPDATE ".$this->table." SET
						nama_gambar = '$nama',
						deskripsi 	= '$deskripsi',					   
						slideshow 	= '$slideshow'					   
					    WHERE id=$id");
	 }
	 
	 function getFotoSlideshow(){
		$query = $this->db->query("SELECT * FROM ".$this->table." WHERE slideshow>0 ORDER BY nama_gambar ASC");
		return $query->result();	
	 }
	 
	 function hitung_slideshow(){
		$data=$this->db->query("SELECT id FROM ".$this->table." WHERE slideshow>0");
		$banyak= $this->db->affected_rows($data);
		return $banyak;
	}
	
	function load_slideshow(){
		$posisi = $this->uri->segment(3);
		if(!is_numeric($posisi))
		{
			$posisi='0';
			$query=$this->db->query("SELECT tbl_foto.*, tbl_album_gambar.nama_album FROM tbl_foto JOIN tbl_album_gambar ON tbl_album_gambar.id = tbl_foto.album_id WHERE tbl_foto.slideshow > 0 ORDER BY tbl_foto.id ASC LIMIT $posisi,10");
			return $query->result();
		}
		if(empty($posisi)) {$posisi = '0';}
		$query=$this->db->query("SELECT tbl_foto.*, tbl_album_gambar.nama_album FROM tbl_foto JOIN tbl_album_gambar ON tbl_album_gambar.id = tbl_foto.album_id WHERE tbl_foto.slideshow > 0 ORDER BY tbl_foto.id ASC LIMIT $posisi,10");
		return $query->result();	
	}
	
	function delete_slideshow($id){
		$this->db->query("UPDATE ".$this->table." SET slideshow = '' WHERE id=$id");
	}
	
	function searchFotoGuest($key){
		$query = $this->db->query("SELECT * FROM ".$this->table." WHERE nama_gambar RLIKE '%$key%' OR deskripsi RLIKE '%$key%'");
		return $query->result();
	}
}
?>